  ################################
  # 08_prepareRollCallMexico-2.R #
  ################################
  
  rm(list=ls())
  print (version)
  
  # platform       aarch64-apple-darwin20      
  # arch           aarch64                     
  # os             darwin20                    
  # system         aarch64, darwin20           
  # status                                     
  # major          4                           
  # minor          1.2                         
  # year           2021                        
  # month          11                          
  # day            01                          
  # svn rev        81115                       
  # language       R                           
  # version.string R version 4.1.2 (2021-11-01)
  # nickname       Bird Hippie          
  
  library (car)
  library (runjags)
  library (coda)
  library (openxlsx)
  library (reshape2)
  library (random)
  library (scales)
  library (withr)
  library (stringr)
  
  # Set working directory
  savePathRep <- "/all_data/"
  setwd (savePathRep)
  
  
  load ("prepared_roll_call_data_mexico.Rdata")
  
  ## 20 samples used in analysis (originally sampled randomly, hardwired here for replication purposes)
  pri.draw <- c(8,21,43,44,51,164,173,192,202,244,271,272,299,303,305,350,352,368,375,391)
  pan.draw <- c(13,53,97,106,121,137,166,168,203,206,209,219,229,263,281,294,304,306,312,345)
  prd.draw <- c(45,82,95,122,191,195,208,210,232,238,247,266,273,316,318,339,350,360,384,388)
  
  for (h in 1:10) {
    
    PRI.pressure.index <- X.pri[pri.draw[h],]
    PAN.pressure.index <- X.pan[pan.draw[h],]
    PRD.pressure.index <- X.prd[prd.draw[h],]
    
    
    pressureMatrix <- rbind (PRI.pressure.index, PAN.pressure.index, PRD.pressure.index)
    forAgainstMatrix <- rbind(PRI.hi.std.LeaderDecision    # 
                              , PAN.hi.std.LeaderDecision  # 
                              , PRD.hi.std.LeaderDecision) # 
    positionMatrix <- rbind(PRILeaderDecision    
                            , PANLeaderDecision  
                            , PRDLeaderDecision) 
    

    ## Check anchor potential of votes
    full.vote.profile <- cbind (round (PRI.majority.size),
                                round (PAN.majority.size),
                                round (PRD.majority.size))
    rownames (full.vote.profile) <- 1:nrow(full.vote.profile)
    colnames (full.vote.profile) <- c("PRI","PAN","PRD")
    panprd.v.pri <- rownames (full.vote.profile[full.vote.profile[,2]==full.vote.profile[,3] & full.vote.profile[,1]!=full.vote.profile[,2],])
    
    potential.fix.bill <- c(22,89,144,150,174,217)

    #### Gather objects that will be turned into vector format ####
    # Vectorize roll call information
    main.party <- c()
    for (i in 1:nrow(full.party.membership)){
      tmp.party <- unique (full.party.membership[i])
      if (length (tmp.party)>1) {
        tmp.party <- min (na.omit (tmp.party)) 
      }
      main.party <- c(main.party, tmp.party)
    }
    
    main.party <-ifelse (is.na(main.party), 8, main.party)
    
    # Change gender matrices
    for (i in 1:nrow(PAN.fem.matrix)){
      female.matrix[i,] <- ifelse (complete.party.ltd$gender[i]==2, rep(1, ncol(full.roll.call))
                                   , rep(0, ncol(full.roll.call)))
    }
    
    
    for (i in 1:nrow(PAN.fem.matrix)){
      for (j in 1:ncol(PAN.fem.matrix)){
        PAN.fem.matrix[i,j] <- ifelse (complete.party.ltd$gender[i]==2 
                                       & full.party.membership[i,j]==2
                                       & !is.na(full.party.membership[i,j]), 1, 0)
        PAN.male.matrix[i,j] <- ifelse (complete.party.ltd$gender[i]==1 
                                        & full.party.membership[i,j]==2
                                        & !is.na(full.party.membership[i,j]), 1, 0)
        PRI.fem.matrix[i,j] <- ifelse (complete.party.ltd$gender[i]==2 
                                       & full.party.membership[i,j]==1
                                       & !is.na(full.party.membership[i,j]), 1, 0)
        PRI.male.matrix[i,j] <- ifelse (complete.party.ltd$gender[i]==1 
                                        & full.party.membership[i,j]==1
                                        & !is.na(full.party.membership[i,j]), 1, 0)
        PRD.fem.matrix[i,j] <- ifelse (complete.party.ltd$gender[i]==2 
                                       & full.party.membership[i,j]==3
                                       & !is.na(full.party.membership[i,j]), 1, 0)
        PRD.male.matrix[i,j] <- ifelse (complete.party.ltd$gender[i]==1 
                                        & full.party.membership[i,j]==3
                                        & !is.na(full.party.membership[i,j]), 1, 0)
      }
    }
    
    
    for (i in 1:nrow(PAN.poverty.matrix)){
      for (j in 1:ncol(PAN.poverty.matrix)){
        PAN.poverty.matrix[i,j] <- ifelse (!is.na(complete.party.ltd$poverty[i]) 
                                           & full.party.membership[i,j]==2
                                           & !is.na(full.party.membership[i,j]), complete.party.ltd$poverty[i], 0)
        PRI.poverty.matrix[i,j] <- ifelse (!is.na(complete.party.ltd$poverty[i]) 
                                           & full.party.membership[i,j]==1
                                           & !is.na(full.party.membership[i,j]), complete.party.ltd$poverty[i], 0)
        PRD.poverty.matrix[i,j] <- ifelse (!is.na(complete.party.ltd$poverty[i]) 
                                           & full.party.membership[i,j]==3
                                           & !is.na(full.party.membership[i,j]), complete.party.ltd$poverty[i], 0)
      }
    }
    
    
    for (i in 1:nrow(PAN.victory.margin.matrix)){
      for (j in 1:ncol(PAN.victory.margin.matrix)){
        PAN.victory.margin.matrix[i,j] <- ifelse (!is.na(complete.party.ltd$mgFirstSecond[i]) 
                                                  & full.party.membership[i,j]==2
                                                  & !is.na(full.party.membership[i,j]), complete.party.ltd$mgFirstSecond[i], NA)
        PRI.victory.margin.matrix[i,j] <- ifelse (!is.na(complete.party.ltd$mgFirstSecond[i]) 
                                                  & full.party.membership[i,j]==1
                                                  & !is.na(full.party.membership[i,j]), complete.party.ltd$mgFirstSecond[i], NA)
        PRD.victory.margin.matrix[i,j] <- ifelse (!is.na(complete.party.ltd$mgFirstSecond[i]) 
                                                  & full.party.membership[i,j]==3
                                                  & !is.na(full.party.membership[i,j]), complete.party.ltd$mgFirstSecond[i], NA)
      }
    }
    
    
    for (i in 1:nrow(PAN.year1.vote.matrix)){
      for (j in 1:ncol(PAN.year1.vote.matrix)){
        PAN.year1.vote.matrix[i,j] <- ifelse (Year.1.vote.matrix[i,j]==1 
                                              & PAN.member.matrix[i,j]==1, 1, 0)
        PAN.year2.vote.matrix[i,j] <- ifelse (Year.2.vote.matrix[i,j]==1 
                                              & PAN.member.matrix[i,j]==1, 1, 0)
        PAN.year3.vote.matrix[i,j] <- ifelse (Year.3.vote.matrix[i,j]==1 
                                              & PAN.member.matrix[i,j]==1, 1, 0)
        PRI.year1.vote.matrix[i,j] <- ifelse (Year.1.vote.matrix[i,j]==1 
                                              & PRI.member.matrix[i,j]==1, 1, 0)
        PRI.year2.vote.matrix[i,j] <- ifelse (Year.2.vote.matrix[i,j]==1 
                                              & PRI.member.matrix[i,j]==1, 1, 0)
        PRI.year3.vote.matrix[i,j] <- ifelse (Year.3.vote.matrix[i,j]==1 
                                              & PRI.member.matrix[i,j]==1, 1, 0)
        PRD.year1.vote.matrix[i,j] <- ifelse (Year.1.vote.matrix[i,j]==1 
                                              & PRD.member.matrix[i,j]==1, 1, 0)
        PRD.year2.vote.matrix[i,j] <- ifelse (Year.2.vote.matrix[i,j]==1 
                                              & PRD.member.matrix[i,j]==1, 1, 0)
        PRD.year3.vote.matrix[i,j] <- ifelse (Year.3.vote.matrix[i,j]==1 
                                              & PRD.member.matrix[i,j]==1, 1, 0)
      }
    }
    
    
    for (i in 1:nrow(PAN.pcbrequest.matrix)){
      for (j in 1:ncol(PAN.year1.vote.matrix)){
        PAN.pcbrequest.matrix[i,j]     <- ifelse (PCB.request[j]==1
                                                  & PAN.member.matrix[i,j]==1, 1, 0)
        PAN.prezposition.matrix[i,j]   <- ifelse (President.position[j]==1
                                                  & PAN.member.matrix[i,j]==1, 1, 0)
        PAN.cabposition.matrix[i,j]    <- ifelse (Cabinet.position[j]==1
                                                  & PAN.member.matrix[i,j]==1, 1, 0)
        PAN.proceduralvote.matrix[i,j] <- ifelse (Procedural.vote[j]==1
                                                  & PAN.member.matrix[i,j]==1, 1, 0)
        PRI.pcbrequest.matrix[i,j]     <- ifelse (PCB.request[j]==1
                                                  & PRI.member.matrix[i,j]==1, 1, 0)
        PRI.prezposition.matrix[i,j]   <- ifelse (President.position[j]==1
                                                  & PRI.member.matrix[i,j]==1, 1, 0)
        PRI.cabposition.matrix[i,j]    <- ifelse (Cabinet.position[j]==1
                                                  & PRI.member.matrix[i,j]==1, 1, 0)
        PRI.proceduralvote.matrix[i,j] <- ifelse (Procedural.vote[j]==1
                                                  & PRI.member.matrix[i,j]==1, 1, 0)
        PRD.pcbrequest.matrix[i,j]     <- ifelse (PCB.request[j]==1
                                                  & PRD.member.matrix[i,j]==1, 1, 0)
        PRD.prezposition.matrix[i,j]   <- ifelse (President.position[j]==1
                                                  & PRD.member.matrix[i,j]==1, 1, 0)
        PRD.cabposition.matrix[i,j]    <- ifelse (Cabinet.position[j]==1
                                                  & PRD.member.matrix[i,j]==1, 1, 0)
        PRD.proceduralvote.matrix[i,j] <- ifelse (Procedural.vote[j]==1
                                                  & PRD.member.matrix[i,j]==1, 1, 0)
      }
    }
    
    
    for (i in 1:nrow(PAN.pcbrequest.matrix)){
      for (j in 1:ncol(PAN.year1.vote.matrix)){
        PCB.matrix[i,j]     <- ifelse (PCB.request[j]==1, 1, 0)
        PrezPosition.matrix[i,j] <- ifelse (President.position[j]==1, 1, 0)
        Year.1.vote.matrix[i,j] <- ifelse (Year.1.vote.matrix[i,j]==1, 1, 0)
        Year.2.vote.matrix[i,j] <- ifelse (Year.2.vote.matrix[i,j]==1, 1, 0)
        Year.3.vote.matrix[i,j] <- ifelse (Year.3.vote.matrix[i,j]==1, 1, 0)
        stateLegislator.matrix[i,j] <- ifelse (complete.party.ltd$State.Legislator[i]==1 
                                               & !is.na(full.party.membership[i,j]), 1, 0)
        committeeChair.matrix[i,j] <- ifelse (complete.party.ltd$Committee.President[i]==1 
                                              & !is.na(full.party.membership[i,j]), 1, 0) 
        Poverty.matrix[i,j] <- ifelse (!is.na(complete.party.ltd$poverty[i]) 
                                       & !is.na(full.party.membership[i,j]), complete.party.ltd$poverty[i], 0)
        Year.matrix[i,j] <- ifelse (Year.1.vote.matrix[i,j]==1, 1, ifelse (Year.1.vote.matrix[i,j]==0 & Year.2.vote.matrix[i,j]==1, 2, 3))
        PR.matrix[i,j] <- ifelse (complete.party.ltd$district.type.r[i]==2 
                                  & !is.na(full.party.membership[i,j]), 1, 0)
      }
    }
    
    for (i in 1:nrow(PAN.pcbrequest.matrix)){
      for (j in 1:ncol(PAN.year1.vote.matrix)){
        PAN.year.vote.matrix[i,j]  <- ifelse (PAN.member.matrix[i,j]==1, Year.matrix[i,j], 0)
        PRI.year.vote.matrix[i,j]  <- ifelse (PRI.member.matrix[i,j]==1, Year.matrix[i,j], 0)
        PRD.year.vote.matrix[i,j]  <- ifelse (PRD.member.matrix[i,j]==1, Year.matrix[i,j], 0)
      }
    }    
    
    for (i in 1:nrow(PAN.pcbrequest.matrix)){
      for (j in 1:ncol(PAN.year1.vote.matrix)){
        victoryMargin[i,j]      <- ifelse (!is.na(complete.party.ltd$mgFirstSecond[i]) 
                                           & !is.na(full.party.membership[i,j]), complete.party.ltd$mgFirstSecond[i], NA)
        proceduralVote[i,j]     <- ifelse (Procedural.vote[j]==1, 1, 0)
        cabinetExp[i,j]    <- ifelse (complete.party.ltd$Cabinet.Position[i]==1
                                      & !is.na(complete.party.ltd$Cabinet.Position[i])
                                      & !is.na(full.party.membership[i,j]), 1, 0)
        executiveExp[i,j]       <- ifelse (complete.party.ltd$executive.exp[i]==1
                                           & !is.na(complete.party.ltd$executive.exp[i])
                                           & !is.na(full.party.membership[i,j]), 1, 0)
      }
    }    
    
    threshold <- quantile (complete.party.ltd$mgFirstSecond, prob=0.25, na.rm=T) #
    PAN.victory.margin.matrix.dummy <- ifelse (PAN.victory.margin.matrix < threshold & !is.na(PAN.victory.margin.matrix), 1, NA)
    PRI.victory.margin.matrix.dummy <- ifelse (PRI.victory.margin.matrix < threshold & !is.na(PRI.victory.margin.matrix), 1, NA)
    PRD.victory.margin.matrix.dummy <- ifelse (PRD.victory.margin.matrix < threshold & !is.na(PRD.victory.margin.matrix), 1, NA)
    victoryMarginDummy <- ifelse (victoryMargin < threshold, 1, 0)
    
    PAN.victory.margin.matrix <- recode (PAN.victory.margin.matrix, "NA=0")
    PRI.victory.margin.matrix <- recode (PRI.victory.margin.matrix, "NA=0")
    PRD.victory.margin.matrix <- recode (PRD.victory.margin.matrix, "NA=0")
    PAN.victory.margin.matrix.dummy <- recode (PAN.victory.margin.matrix.dummy, "NA=0")
    PRI.victory.margin.matrix.dummy <- recode (PRI.victory.margin.matrix.dummy, "NA=0")
    PRD.victory.margin.matrix.dummy <- recode (PRD.victory.margin.matrix.dummy, "NA=0")
    victoryMargin <- recode (victoryMargin, "NA=0")
    victoryMarginDummy <- recode (victoryMarginDummy, "NA=0")
    
    PRI.substituteLegs <- PAN.substituteLegs <- PRD.substituteLegs <- substituteLegs
    for (i in 1:nrow(PAN.pcbrequest.matrix)){
      for (j in 1:ncol(PAN.year1.vote.matrix)){
        substituteLegs[i,j]      <- ifelse (is.na(complete.party.ltd$mgFirstSecond[i]) 
                                            & !is.na(full.party.membership[i,j]), 1, 0)
        PAN.substituteLegs[i,j] <- ifelse (is.na(complete.party.ltd$mgFirstSecond[i]) 
                                           & full.party.membership[i,j]==2
                                           & !is.na(full.party.membership[i,j]), 1, 0)
        PRI.substituteLegs[i,j] <- ifelse (is.na(complete.party.ltd$mgFirstSecond[i]) 
                                           & full.party.membership[i,j]==1
                                           & !is.na(full.party.membership[i,j]), 1, 0)
        PRD.substituteLegs[i,j] <- ifelse (is.na(complete.party.ltd$mgFirstSecond[i]) 
                                           & full.party.membership[i,j]==3
                                           & !is.na(full.party.membership[i,j]), 1, 0)
      }
    }    
    
    cbind (PAN.substituteLegs[,20], PRI.substituteLegs[,20], PRD.substituteLegs[,20], substituteLegs[,20])
    
    ##############
    # Quinones Cornejo Maria de la Paz: Change "main.party[336]" to 8 (independent)
    # Cirigo Vasquez Victor Hugo: Change "main.party[c(392,393)]" to 4 (MC)
    # Pina Olmedo Laura: Change "main.party[437]" to 4
    main.party[which (deputy.names.roll.call=="Quinones Cornejo Maria de la Paz")[2]] <- 8
    main.party[which (deputy.names.roll.call=="Cirigo Vasquez Victor Hugo")[c(2,3)]] <- 4
    main.party[which (deputy.names.roll.call=="Pina Olmedo Laura")[c(2,3)]] <- 4
    
    all.party.colors <- car::recode (main.party, "1='green';
                         2='blue';
                         3='red';
                         4='lightgreen';
                         5='orange';
                         6='black';
                         7='purple';
                         8='gray'")
    colors <- ifelse (main.party==1, "green", ifelse (main.party==2, "blue", ifelse (main.party==3,"red","grey")))
    
    party.order <- order (main.party) # send non-PRI/PAN/PRD to end
    deputy.names.party.order <- factor (deputy.names.roll.call[party.order])
    
    
    
    # Calculate party centroids, party variances
    party.memb <- main.party 
    ########################################################

    pressureMatrix.exp <-  rbind (pressureMatrix, rep(0,ncol(pressureMatrix)), rep(0,ncol(pressureMatrix)), rep(0,ncol(pressureMatrix)), rep(0,ncol(pressureMatrix)), rep(0,ncol(pressureMatrix))) # pressureMatrix # 
    
    positionMatrix.exp <- rbind (positionMatrix, rep(0,ncol(positionMatrix)), rep(0,ncol(positionMatrix)), rep(0,ncol(positionMatrix)), rep(0,ncol(positionMatrix)), rep(0,ncol(positionMatrix))) # positionMatrix #  
    forAgainstMatrix.exp <- rbind (forAgainstMatrix, rep(0,ncol(forAgainstMatrix)), rep(0,ncol(forAgainstMatrix)), rep(0,ncol(forAgainstMatrix)), rep(0,ncol(forAgainstMatrix)), rep(0,ncol(forAgainstMatrix)))
    
    
    PAN.poverty.matrix.dummy <- ifelse (PAN.poverty.matrix > quantile (complete.party.ltd$poverty, prob=0.75, na.rm=T), 1, 0)
    PRI.poverty.matrix.dummy <- ifelse (PRI.poverty.matrix > quantile (complete.party.ltd$poverty, prob=0.75, na.rm=T), 1, 0)
    PRD.poverty.matrix.dummy <- ifelse (PRD.poverty.matrix > quantile (complete.party.ltd$poverty, prob=0.75, na.rm=T), 1, 0)
    
    Poverty.matrix.dummy <- ifelse (Poverty.matrix > quantile (complete.party.ltd$poverty, prob=0.75, na.rm=T), 1, 0)
    
    # The following Rmat object just puts the PR.matrices for all parties together
    Rmat <- array (dim=c(88
                         ,nrow(full.roll.call)
                         ,ncol(full.roll.call)))
    
    
    Rmat[1,,] <- PRI.PR.matrix
    Rmat[2,,] <- PAN.PR.matrix
    Rmat[3,,] <- PRD.PR.matrix
    Rmat[4,,] <- PRI.SMD.matrix
    Rmat[5,,] <- PAN.SMD.matrix
    Rmat[6,,] <- PRD.SMD.matrix
    Rmat[7,,] <- PRI.member.matrix
    Rmat[8,,] <- PAN.member.matrix
    Rmat[9,,] <- PRD.member.matrix
    Rmat[10,,] <- female.matrix
    Rmat[11,,] <- PAN.poverty.matrix  # includes continuous variables
    Rmat[12,,] <- PRI.poverty.matrix  # includes continuous variables
    Rmat[13,,] <- PRD.poverty.matrix  # includes continuous variables
    Rmat[14,,] <- PAN.victory.margin.matrix  # includes continuous variables
    Rmat[15,,] <- PRI.victory.margin.matrix  # includes continuous variables
    Rmat[16,,] <- PRD.victory.margin.matrix  # includes continuous variables
    Rmat[17,,] <- PAN.year1.vote.matrix
    Rmat[18,,] <- PAN.year2.vote.matrix
    Rmat[19,,] <- PAN.year3.vote.matrix
    Rmat[20,,] <- PRI.year1.vote.matrix
    Rmat[21,,] <- PRI.year2.vote.matrix
    Rmat[22,,] <- PRI.year3.vote.matrix
    Rmat[23,,] <- PRD.year1.vote.matrix
    Rmat[24,,] <- PRD.year2.vote.matrix
    Rmat[25,,] <- PRD.year3.vote.matrix
    Rmat[26,,] <- PAN.poverty.matrix.dummy  # includes dummies
    Rmat[27,,] <- PRI.poverty.matrix.dummy  # includes dummies
    Rmat[28,,] <- PRD.poverty.matrix.dummy  # includes dummies
    Rmat[29,,] <- PAN.victory.margin.matrix.dummy  # includes dummies
    Rmat[30,,] <- PRI.victory.margin.matrix.dummy  # includes dummies
    Rmat[31,,] <- PRD.victory.margin.matrix.dummy  # includes dummies
    Rmat[32,,] <- PAN.pcbrequest.matrix
    Rmat[33,,] <- PAN.prezposition.matrix
    Rmat[34,,] <- PAN.cabposition.matrix
    Rmat[35,,] <- PAN.proceduralvote.matrix
    Rmat[36,,] <- PRI.pcbrequest.matrix
    Rmat[37,,] <- PRI.prezposition.matrix
    Rmat[38,,] <- PRI.cabposition.matrix
    Rmat[39,,] <- PRI.proceduralvote.matrix
    Rmat[40,,] <- PRD.pcbrequest.matrix
    Rmat[41,,] <- PRD.prezposition.matrix
    Rmat[42,,] <- PRD.cabposition.matrix
    Rmat[43,,] <- PRD.proceduralvote.matrix
    Rmat[44,,] <- PAN.commPres.matrix
    Rmat[45,,] <- PRI.commPres.matrix
    Rmat[46,,] <- PRD.commPres.matrix
    Rmat[47,,] <- PAN.cabinet.matrix
    Rmat[48,,] <- PRI.cabinet.matrix
    Rmat[49,,] <- PRD.cabinet.matrix
    Rmat[50,,] <- PAN.state.muni.leg.matrix
    Rmat[51,,] <- PRI.state.muni.leg.matrix
    Rmat[52,,] <- PRD.state.muni.leg.matrix
    Rmat[53,,] <- PAN.exec.exp.matrix
    Rmat[54,,] <- PRI.exec.exp.matrix
    Rmat[55,,] <- PRD.exec.exp.matrix
    Rmat[56,,] <- PCB.matrix
    Rmat[57,,] <- PrezPosition.matrix
    Rmat[58,,] <- Year.1.vote.matrix
    Rmat[59,,] <- Year.2.vote.matrix
    Rmat[60,,] <- Year.3.vote.matrix
    Rmat[61,,] <- stateLegislator.matrix
    Rmat[62,,] <- committeeChair.matrix 
    Rmat[63,,] <- Poverty.matrix
    Rmat[64,,] <- Poverty.matrix.dummy
    Rmat[65,,] <- Year.matrix
    Rmat[66,,] <- Year.matrix
    Rmat[67,,] <- PR.matrix
    Rmat[68,,] <- PRI.year.vote.matrix
    Rmat[69,,] <- PAN.year.vote.matrix
    Rmat[70,,] <- PRD.year.vote.matrix
    Rmat[71,,] <- PrezPosition.matrix
    Rmat[72,,] <- stateLegislator.matrix
    Rmat[73,,] <- committeeChair.matrix 
    Rmat[74,,] <- victoryMargin
    Rmat[75,,] <- victoryMarginDummy
    Rmat[76,,] <- proceduralVote
    Rmat[77,,] <- cabinetExp
    Rmat[78,,] <- executiveExp
    Rmat[79,,] <- victoryMargin
    Rmat[80,,] <- victoryMarginDummy
    Rmat[81,,] <- proceduralVote
    Rmat[82,,] <- cabinetExp
    Rmat[83,,] <- executiveExp 
    Rmat[84,,] <- PAN.substituteLegs
    Rmat[85,,] <- PRI.substituteLegs
    Rmat[86,,] <- PRD.substituteLegs
    Rmat[87,,] <- substituteLegs
    Rmat[88,,] <- substituteLegs
    Rmat.exp <- Rmat# 
    
    pressure.for <- pressureMatrix.exp*positionMatrix.exp
    high.pressure.for <- pressureMatrix.exp*forAgainstMatrix.exp
    
    party.pressure.for <- matrix (NA, ncol=ncol(full.party.membership), nrow=nrow(full.party.membership))
    for (i in 1:nrow(full.party.membership)){
      for (j in 1:ncol(full.party.membership)){
        party.pressure.for[i,j] <- ifelse (full.party.membership[i,j]==1 & !is.na(full.party.membership[i,j]), pressure.for[1,j]
                                           , ifelse (full.party.membership[i,j]==2 & !is.na(full.party.membership[i,j]), pressure.for[2,j]
                                                     , ifelse (full.party.membership[i,j]==3 & !is.na(full.party.membership[i,j]), pressure.for[3,j], 0)))
      }
    }
    
    high.party.pressure.for <- matrix (NA, ncol=ncol(full.party.membership), nrow=nrow(full.party.membership))
    for (i in 1:nrow(full.party.membership)){
      for (j in 1:ncol(full.party.membership)){
        high.party.pressure.for[i,j] <- ifelse (full.party.membership[i,j]==1 & !is.na(full.party.membership[i,j]), high.pressure.for[1,j]
                                                , ifelse (full.party.membership[i,j]==2 & !is.na(full.party.membership[i,j]), high.pressure.for[2,j]
                                                          , ifelse (full.party.membership[i,j]==3 & !is.na(full.party.membership[i,j]), high.pressure.for[3,j], 0)))
      }
    }
    
    
    
    R1 <- R2 <- R3 <- R4 <- R5 <- R6 <- R7 <- R8 <- R9 <- R10 <- R11 <- R12 <- R13 <- R14 <- R15 <- R16 <-
      R17 <- R18 <- R19 <- R20 <- R21 <- R22 <- R23 <- R24 <- R25 <- R26 <- R27 <- R28 <- R29 <- R30 <- R31 <- 
      R32 <- R33 <- R34 <- R35 <- R36 <- R37 <- R38 <- R39 <- R40 <- R41 <- R42 <- R43 <-
      R44 <- R45 <- R46 <- R47 <- R48 <- R49 <- R50 <- R51 <- R52 <- R53 <- R54 <- R55 <- 
      R56 <- R57 <- R58 <- R59 <- R60 <- R61 <- R62 <- R63 <- R64 <- R65 <- R66 <- R67 <-
      R68 <- R69 <- R70 <- R71 <- R72 <- R73 <- R74 <- R75 <- R76 <- R77 <-
      R78 <- R79 <- R80 <- R81 <- R82 <- R83 <- R84 <- R85 <- R86 <- R87 <- R88 <- matrix (NA, ncol=ncol(full.party.membership), nrow=nrow(full.party.membership))

    R1 <- Rmat.exp[1,,] * party.pressure.for #PRI PR  
    R2 <- Rmat.exp[2,,] * party.pressure.for #PAN PR  
    R3 <- Rmat.exp[3,,] * party.pressure.for #PRD PR  
    R4 <- Rmat.exp[4,,] * party.pressure.for #PRI SMD  
    R5 <- Rmat.exp[5,,] * party.pressure.for #PAN SMD  
    R6 <- Rmat.exp[6,,] * party.pressure.for #PRD SMD   
    R7 <- Rmat.exp[7,,] * party.pressure.for # PRI 
    R8 <- Rmat.exp[8,,] * party.pressure.for # PAN   
    R9 <- Rmat.exp[9,,] * party.pressure.for # PRD   
    R10 <- Rmat.exp[10,,] * party.pressure.for # actual party   
    R11 <- Rmat.exp[11,,] * party.pressure.for # actual party   
    R12 <- Rmat.exp[12,,] * party.pressure.for # actual party   
    R13 <- Rmat.exp[13,,] * party.pressure.for # actual party   
    R14 <- Rmat.exp[14,,] * party.pressure.for # actual party   
    R15 <- Rmat.exp[15,,] * party.pressure.for # actual party   
    R16 <- Rmat.exp[16,,] * party.pressure.for # actual party   
    R17 <- Rmat.exp[17,,] * party.pressure.for # actual party   
    R18 <- Rmat.exp[18,,] * party.pressure.for # actual party   
    R19 <- Rmat.exp[19,,] * party.pressure.for # actual party   
    R20 <- Rmat.exp[20,,] * party.pressure.for # actual party   
    R21 <- Rmat.exp[21,,] * party.pressure.for # actual party   
    R22 <- Rmat.exp[22,,] * party.pressure.for # actual party   
    R23 <- Rmat.exp[23,,] * party.pressure.for # actual party   
    R24 <- Rmat.exp[24,,] * party.pressure.for # actual party   
    R25 <- Rmat.exp[25,,] * party.pressure.for # actual party   
    R26 <- Rmat.exp[26,,] * party.pressure.for # actual party   
    R27 <- Rmat.exp[27,,] * party.pressure.for # actual party   
    R28 <- Rmat.exp[28,,] * party.pressure.for # actual party   
    R29 <- Rmat.exp[29,,] * party.pressure.for # actual party   
    R30 <- Rmat.exp[30,,] * party.pressure.for # actual party   
    R31 <- Rmat.exp[31,,] * party.pressure.for # actual party   
    R32 <- Rmat.exp[32,,] * party.pressure.for # actual party   
    R33 <- Rmat.exp[33,,] * party.pressure.for # actual party   
    R34 <- Rmat.exp[34,,] * party.pressure.for # actual party   
    R35 <- Rmat.exp[35,,] * party.pressure.for # actual party   
    R36 <- Rmat.exp[36,,] * party.pressure.for # actual party   
    R37 <- Rmat.exp[37,,] * party.pressure.for # actual party   
    R38 <- Rmat.exp[38,,] * party.pressure.for # actual party   
    R39 <- Rmat.exp[39,,] * party.pressure.for # actual party   
    R40 <- Rmat.exp[40,,] * party.pressure.for # actual party   
    R41 <- Rmat.exp[41,,] * party.pressure.for # actual party   
    R42 <- Rmat.exp[42,,] * party.pressure.for # actual party   
    R43 <- Rmat.exp[43,,] * party.pressure.for # actual party   
    R44 <- Rmat.exp[44,,] * party.pressure.for # actual party   
    R45 <- Rmat.exp[45,,] * party.pressure.for # actual party   
    R46 <- Rmat.exp[46,,] * party.pressure.for # actual party   
    R47 <- Rmat.exp[47,,] * party.pressure.for # actual party   
    R48 <- Rmat.exp[48,,] * party.pressure.for # actual party   
    R49 <- Rmat.exp[49,,] * party.pressure.for # actual party   
    R50 <- Rmat.exp[50,,] * party.pressure.for # actual party   
    R51 <- Rmat.exp[51,,] * party.pressure.for # actual party   
    R52 <- Rmat.exp[52,,] * party.pressure.for # actual party   
    R53 <- Rmat.exp[53,,] * party.pressure.for # actual party   
    R54 <- Rmat.exp[54,,] * party.pressure.for # actual party   
    R55 <- Rmat.exp[55,,] * party.pressure.for # actual party   
    R56 <- Rmat.exp[56,,] 
    R57 <- Rmat.exp[57,,] * party.pressure.for # actual party    
    R58 <- Rmat.exp[58,,] 
    R59 <- Rmat.exp[59,,] 
    R60 <- Rmat.exp[60,,] 
    R61 <- Rmat.exp[61,,] * party.pressure.for # actual party   
    R62 <- Rmat.exp[62,,] * party.pressure.for # actual party   
    R63 <- Rmat.exp[63,,] 
    R64 <- Rmat.exp[64,,] 
    R65 <- Rmat.exp[65,,] 
    R66 <- Rmat.exp[66,,] * party.pressure.for # actual party   
    R67 <- Rmat.exp[67,,] 
    R68 <- Rmat.exp[68,,] * party.pressure.for # actual party   
    R69 <- Rmat.exp[69,,] * party.pressure.for # actual party   
    R70 <- Rmat.exp[70,,] * party.pressure.for # actual party   
    R71 <- Rmat.exp[71,,] 
    R72 <- Rmat.exp[72,,] 
    R73 <- Rmat.exp[73,,] 
    R74 <- Rmat.exp[74,,] 
    R75 <- Rmat.exp[75,,] 
    R76 <- Rmat.exp[76,,]
    R77 <- Rmat.exp[77,,] 
    R78 <- Rmat.exp[78,,]
    R79 <- Rmat.exp[79,,] * party.pressure.for # actual party   
    R80 <- Rmat.exp[80,,] * party.pressure.for # actual party   
    R81 <- Rmat.exp[81,,] * party.pressure.for # actual party   
    R82 <- Rmat.exp[82,,] * party.pressure.for # actual party    
    R83 <- Rmat.exp[83,,] * party.pressure.for # actual party    
    R84 <- Rmat.exp[84,,] * party.pressure.for # actual party   
    R85 <- Rmat.exp[85,,] * party.pressure.for # actual party   
    R86 <- Rmat.exp[86,,] * party.pressure.for # actual party   
    R87 <- Rmat.exp[87,,] * party.pressure.for # actual party    
    R88 <- Rmat.exp[88,,] 
    
    path <- paste0 ("mexico_rollcall_intermediate_", h, ".RData")
    # Uncomment next line to save all "mexico_rollcall_intermediate_h.RData" files
    # save.image (file=path)
    print (paste0 ("Sample ", h, " processed"))
  }
  
